$(function() {
	initZtree();
	// initUnbindUser();
});
var level;
var serverGridData = {};
var serverGridTable;
var serverUnbindUserData = {};
var serverUnbindUserTable;
var serverbindUserData = {};
var serverbindUserTable;
// 初始化 层级树
var primaryNode = [];
// //////////////初始化模型的树结构/////////////////////////
var modalSetting = {
	edit : {
		enable : true,
		showRemoveBtn : false,
		showRenameBtn : false,
		drag : {
			isCopy : false,
			isMove : false,
			prev : false,
			next : false,
			inner : false
		}
	},
	data : {
		simpleData : {
			enable : true,
			idKey : "id",
			pIdKey : "parentId",
			rootParentId : 0
		}
	},
	view : {
		showLine : true
	},
	check : {
		enable : true,
		chkStyle : "radio",
		radioType : "level"
	},
	callback : {
		onClick : showBindGridPrepare,
		onCheck : editNewModule
	}

};
// ============================解除绑定==================
$(".unbindGrid").click(
		function() {
			$("#selectbindUser").removeAttr("checked");
			var userIds = '';
			$(".selectbindUser").each(function() {
				if ($(this).prop("checked") == true) {
					userIds += $(this).attr("ownId") + ",";
				}
			});

			if (userIds == '') {
				message("请勾选要解除绑定的用户!");
				return false;
			}
			var gridId = $("#unbindGridId").val();
			$("#right").mLoading("show");
			$.ajax({
				url : rootPath + "/ui/userGridDivison/unbind/" + userIds + "/"
						+ gridId,
				dataType : 'json',
				type : 'POST',
				success : function(result) {
					if (result.code == 1) {
						message("解绑成功!");
						serverUnbindUserData.key = '';
						serverUnbindUserTable.draw();

						serverbindUserData.key = gridId;
						serverbindUserTable.draw();
					}
					$("#right").mLoading("hide");
				}
			});
		});
// ==========================用户绑定网格========================
$(".bindGrid").click(function() {
	$("#selectUnbindUser").removeAttr("checked");
	// 获取勾选的网格
	var userIds = '';
	$(".selectAllUnbindUser").each(function() {
		if ($(this).prop("checked") == true) {
			userIds += $(this).attr("ownId") + ",";
		}
	});
	if (userIds == '') {
		message("请勾选要绑定的用户!");
		return false;
	}
	var gridId = $("#bindGridId").val();
	if (gridId == '') {
		message("请点击网格编码进行绑定!");
		return false;
	}
	$("#right").mLoading("show");
	$.ajax({
		url : rootPath + "/ui/userGridDivison/bind/" + gridId + "/" + userIds,
		dataType : 'json',
		type : 'get',
		success : function(result) {
			if (result.code == 1) {
				message("绑定成功!");
				serverUnbindUserData.key = '';
				serverUnbindUserTable.draw();

				serverbindUserData.key = gridId;
				serverbindUserTable.draw();
			}
			$("#right").mLoading("hide");
		}
	});
});
// ============================未绑定网格的用户=======================
$(".searchBtn2").click(function() {
	 $("#selectUnbindUser").removeAttr("checked");
	serverUnbindUserData.key = $("#search2").val();
	var gridId = $("#gridId").val();

	if (gridId == '') {
		gridId = '0';
	}
	serverUnbindUserData.gid = gridId;
	serverUnbindUserTable.draw();
});
$(".resetBtn2").click(function() {
	 $("#selectUnbindUser").removeAttr("checked");
	$("#search2").val('');
	serverUnbindUserData.key = '';
	var gridId = $("#gridId").val();
	if (gridId == '') {
		gridId = '0';
	}

	serverUnbindUserData.gid = gridId;
	serverUnbindUserTable.draw();
});
function initUnbindUser(id) {

	$("#selectUnbindUser").click(function() {
		var state = $(this).prop("checked");
		$(".selectAllUnbindUser").prop("checked", state);
	});
	serverUnbindUserTable = jQuery('#dataUnbindUserTable')
			.DataTable(
					{
						"serverSide" : true,
						"ordering" : false,
						"ajax" : {
							"dataType" : "json",
							"type" : "post",
							"cache" : false,
							"url" : window.rootPath
									+ '/ui/userGridDivison/getUnbindUserPage',
							"data" : function(d) {
								return $.extend({}, d, serverUnbindUserData);
							}
						},
						columns : [
								{
									data : null,
									defaultContent : ""
								},
								{
									data : 'id',
									defaultContent : "",
									render : function(data, type, full, meta) {

										var operate = "<input type='checkbox' class='selectAllUnbindUser' ownId='"
												+ data + "'/>";
										return operate;
									}
								}, {
									data : "name"
								} ],
						"drawCallback" : function(oSettings) {
							var k = 0;
							var total = oSettings.aiDisplay.length
									+ oSettings._iDisplayStart;
							for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
								$(
										'td:eq(0)',
										oSettings.aoData[oSettings.aiDisplay[k]].nTr)
										.text(i + 1);
								k++;
							}
							serverUnbindUserTable.off('click', 'tr');

						}
					});
}
function initBindUser(id) {
	serverbindUserData.key = id;
	initBindUserPage(id);
	serverbindUserTable.draw();
};
// ====================================已绑定网格的用户====================================
$(".searchBtn3").click(function() {
	$("#selectbindUser").removeAttr("checked");
	serverbindUserData.con = $("#search3").val();

	var val = $("#gridId").val();
	if (val == '') {
		val = "0";
	}
	serverbindUserData.key = val;// 网格id

	serverbindUserTable.draw();
});
$(".resetBtn3").click(function() {
	$("#selectbindUser").removeAttr("checked");
	$("#search3").val('');
	var val = $("#gridId").val();
	if ('' == val) {
		val = "0";
	}
	serverbindUserData.con = '';
	serverbindUserData.key = val;// 网格id
	serverbindUserTable.draw();
});
// ===========================已绑定网格的用户================================
function initBindUserPage(id) {

	$("#selectbindUser").click(function() {
		var state = $(this).prop("checked");
		$(".selectbindUser").prop("checked", state);
	});

	serverbindUserTable = jQuery('#dataBindUserTable')
			.DataTable(
					{
						"serverSide" : true,
						"ordering" : false,
						"ajax" : {
							"dataType" : "json",
							"type" : "post",
							"cache" : false,
							"url" : window.rootPath
									+ '/ui/userGridDivison/getBindUserPage',
							"data" : function(d) {
								return $.extend({}, d, serverbindUserData);
							}
						},
						columns : [
								{
									data : null,
									defaultContent : ""
								},
								{
									data : 'id',
									defaultContent : "",
									render : function(data, type, full, meta) {

										var operate = "<input type='checkbox' class='selectbindUser' ownId='"
												+ data + "'/>";
										return operate;
									}
								}, {
									data : "name"
								} ],
						"drawCallback" : function(oSettings) {
							var k = 0;
							var total = oSettings.aiDisplay.length
									+ oSettings._iDisplayStart;
							for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
								$(
										'td:eq(0)',
										oSettings.aoData[oSettings.aiDisplay[k]].nTr)
										.text(i + 1);
								k++;
							}
							serverbindUserTable.off('click', 'tr');

						}
					});

};
function editNewModule(e, treeId, treeNode) {
	// 预留
};
function showBindGridPrepare(e, treeId, treeNode) {

	var divisionId = treeNode.id;

	serverGridData.key = divisionId;
	initGridPage(divisionId);

	// 刷新
	serverbindUserData.key = '';
	if (serverbindUserTable)
		// serverbindUserTable
		serverbindUserTable.draw(false);

	serverUnbindUserData.gid = '';
	if (serverUnbindUserTable)
		serverUnbindUserTable.draw(false);

	serverGridTable.draw();

};
// ===============================绑定了区域的网格信息=============================
$(".searchBtn1").click(function() {
	serverGridData.con = $("#search1").val();
	var treeObj = $.fn.zTree.getZTreeObj("divisionTree");
	var selectNodes = treeObj.getSelectedNodes(treeObj);
	if (selectNodes) {
		var selectNode = selectNodes[0];
		if (selectNode) {
			serverGridData.key = selectNode.id;
		} else {
			serverGridData.key = '0';
		}
	}

	serverGridTable.draw();
});
$(".resetBtn1").click(function() {
	$("#search1").val('');

	serverGridData.con = '';
	var treeObj = $.fn.zTree.getZTreeObj("divisionTree");
	var selectNodes = treeObj.getSelectedNodes(treeObj);
	if (selectNodes) {
		var selectNode = selectNodes[0];
		if (selectNode) {
			serverGridData.key = selectNode.id;
		} else {
			serverGridData.key = '0';
		}
	}
	serverGridTable.draw();
});
function initGridPage(divisionId) {
	serverGridTable = jQuery('#dataGridTable')
			.DataTable(
					{
						"serverSide" : true,
						"ordering" : false,
						"ajax" : {
							"dataType" : "json",
							"type" : "post",
							"cache" : false,
							"url" : window.rootPath
									+ '/ui/userGridDivison/getGridPage',
							"data" : function(d) {
								return $.extend({}, d, serverGridData);
							}
						},
						columns : [
								{
									data : null,
									defaultContent : ""
								},
								{
									data : 'gridIdentify',
									render : function(data, type, full, meta) {
										return "<a style='cursor:pointer' class='aClick'>"
												+ data + "</a>";
									}

								}, {
									data : 'gridName'
								}, {
									data : 'gridArea'
								}, {
									data : 'gridState.title'
								}, {
									data : 'typeOpt.title'
								}, {
									data : 'gridToArcGisObjectId'
								}, {
									data : "division.divisionName"
								} ],
						"drawCallback" : function(oSettings) {
							var k = 0;
							var total = oSettings.aiDisplay.length
									+ oSettings._iDisplayStart;
							for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
								$(
										'td:eq(0)',
										oSettings.aoData[oSettings.aiDisplay[k]].nTr)
										.text(i + 1);
								k++;
							}
							serverGridTable.off('click', 'tr');

							serverGridTable.on('click', 'tr', function(event) {
								event = event || window.event;
								var target = event.target || event.scrElement;
								var data = serverGridTable.row(this).data();

								if (target.className.indexOf('aClick') != -1) {
									$("#gridId").val(data.id);
									$("#bindGridId").val(data.id);
									$("#unbindGridId").val(data.id);
									initBindUser(data.id);

									serverUnbindUserData.gid = data.id;
									initUnbindUser(data.id);
									serverUnbindUserTable.draw();

								}

								return;
							});
						}
					});

};
function initZtree() {

	$.ajax({
		url : rootPath + "/ui/userGridDivison/getAllNodes",
		cache : false,
		type : "get",
		data : '',
		dataType : "json",
		contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
		success : function(data) {
			primaryNode.push({
				id : 0,
				name : "区域层级",
				open : "true"

			});
			for (var i = 0; i < data.nodes.length; i++) {
				primaryNode.push(data.nodes[i]);
			}
			$.fn.zTree.init($("#divisionTree"), modalSetting, primaryNode);
		}
	});
};
